program Sum;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  pelement = ^element;
  element = record
    k, s: Integer;
    next: pelement;
  end;

procedure AddToBeginning (var head: pelement; num1, num2: integer);
var
  tmp: pelement;
begin
  tmp:=head;
  new(head);
  head^.next:=tmp;
  head^.k:=num1;
  head^.s:=num2;
end;

procedure AddToEnd (var head: pelement; num1, num2: integer);
var
  current: pelement;
begin
  if head <> nil then
  begin
    current:=head;
    while (current^.next <> nil) do
    begin
      current:=current^.next;
    end;
    new (current^.next);
    current^.next^.k:=num1;
    current^.next^.s:=num2;
    current^.next^.next:=nil;
  end else begin
    AddToBeginning(head, num1, num2);
  end;
end;

procedure PrintList (head: pelement);
var
  current: pelement;
begin
  current:=head;
  while (current <> nil) do
  begin
    Writeln (current^.k, ' ', current^.s);
    current:=current^.next;
  end;
end;

var
  fin: TextFile;
  flag: Boolean;
  n, m, i, a, k, b, p, c, f: Integer;
  current1, current2, head1, head2, head3: pelement;

begin
  AssignFile(fin, 'input.txt');
  Reset(fin);
  head1:=nil;
  head2:=nil;
  head3:=nil;
  read(fin, n);
  for i:=1 to n do
  begin
    read (fin, a);
    read (fin, k);
    AddToEnd(head1, a, k);
  end;
  read(fin, m);
  for i:=1 to m do
  begin
    read (fin, b);
    read (fin, p);
    AddToEnd(head2, b, p);
  end;
  current1:=head1;
  current2:=head2;
  flag:=False;
  while (current1 <> nil) and (current2 <> nil) do
  begin
    if current1^.s > current2^.s then
    begin
      f:=current1^.s;
      c:=current1^.k;
      current1:=current1^.next;
      flag:=True;
    end;
    if flag = False then
    begin
      if current1^.s < current2^.s then
      begin
        f:=current2^.s;
        c:=current2^.k;
        current2:=current2^.next;
        flag:=True;
      end;
    end;
    if flag = False then
    begin
      if current1^.s = current2^.s then
      begin
        f:=current1^.s;
        c:=current1^.k + current2^.k;
        current1:=current1^.next;
        current2:=current2^.next;
      end;
    end;
    AddToEnd(head3, c, f);
    flag:=False;
  end;
  if (current1 = nil) and (current2 <> nil) then
  begin
    while (current2 <> nil) do
    begin
      f:=current2^.s;
      c:=current2^.k;
      AddToEnd(head3, c, f);
      current2:=current2^.next;
    end;
  end;
  if (current1 <> nil) and (current2 = nil) then
  begin
    while (current1 <> nil) do
    begin
      f:=current1^.s;
      c:=current1^.k;
      AddToEnd(head3, c, f);
      current1:=current1^.next;
    end;
  end;
  PrintList(head3);
  CloseFile(fin);
  readln;
end.
